//
//  DisTableViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/9.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DisTableViewController.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
#import "DisTab.h"
#import "DisTAbCollectionViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "INspectDatilViewController.h"
@interface DisTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *distableview;
@property(nonatomic,retain)NSMutableArray *dibtbArr;
@property(nonatomic,assign)NSInteger page;
@end

@implementation DisTableViewController
- (void)dealloc
{
    [_dibtbArr release];
    [_distableview release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dibtbArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [self creatDisTabView];
    [self PleaseDisTabData];
    [self shuaxin];
}
//创建检查项目视图
-(void)creatDisTabView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing = 10;
    flow.minimumLineSpacing = 25;
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 25, 10);
    flow.itemSize = CGSizeMake(90,100);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.distableview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flow];
    self.distableview.backgroundColor = [UIColor whiteColor];
    [_distableview release];
    self.distableview.delegate = self;
    self.distableview.dataSource = self;
    [self.view addSubview:self.distableview];
    //注册cell
    [self.distableview registerClass:[DisTAbCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
#pragma mark 实现协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dibtbArr.count;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    DisTAbCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    DisTab *dt = self.dibtbArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",dt.img];
    NSURL *url = [NSURL URLWithString:str];
    [cell.picView sd_setImageWithURL:url];
    cell.nameLabel.text = dt.name;
    return cell;
}
//请求检查数据
-(void)PleaseDisTabData
{
    NSString *url = [NSString stringWithFormat:@"http://api.yi18.net/check/list?page=%ld&limit=20&id=%ld",self.page,self.did];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *temp in arr) {
            DisTab *dt = [[DisTab alloc]init];
            [dt setValuesForKeysWithDictionary:temp];
            [self.dibtbArr addObject:dt];
//            [dt release];
            NSLog(@"id===%ld",dt.id);
        }
        [self.distableview reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"疾病信息列表数据请求失败");
    }];
}
//加载刷新
-(void)shuaxin
{
    [self.distableview addLegendHeaderWithRefreshingBlock:^{
        [self.dibtbArr removeAllObjects];
        self.page = 1;
        [self PleaseDisTabData];
        [self.distableview reloadData];
        [self.distableview.header endRefreshing];
    }];
    [self.distableview addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self PleaseDisTabData];
        [self.distableview reloadData];
        [self.distableview.footer endRefreshing];
        
    }];
}
//跳转到检查项目详情列表页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    INspectDatilViewController *indvc = [[INspectDatilViewController alloc]init];
    DisTab *dt = self.dibtbArr[indexPath.row];
    indvc.id = dt.id;
    [self.navigationController pushViewController:indvc animated:YES];
    [indvc release];
    [dt release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
